<template>
	<view class="garden-page">
		<view class="header">
            <image class="gd-icon" @click="goHome" src="https://xwj-miniprogram.oss-cn-hangzhou.aliyuncs.com/static/images/gd.png" mode=""></image>
			<text class="title">我的报名</text>
		</view>
		
		<!-- 报名列表 -->
		<view class="content">
			<view class="registration-list">
				<view class="registration-card" v-for="(item, index) in enrollList" :key="index">
					<!-- 第一行：门店信息和状态 -->
					<view class="card-header">
						<view class="shop-info">
							<image class="shop-icon" src="https://xwj-miniprogram.oss-cn-hangzhou.aliyuncs.com/static/images/gd-mr-md.png" mode=""></image>
							<text class="shop-name">{{ item.merchant_name }}</text>
						</view>
						<text class="status" :class="getStatusClass(item.status)">{{ formatStatus(item.status) }}</text>
					</view>
					
					<!-- 第二行：活动图片和详情 -->
					<view class="card-content">
						<image class="activity-image" :src="item.cover_img" mode="aspectFill"></image>
						<view class="activity-info">
							<text class="activity-title">{{ item.activity_title }}</text>
							<text class="order-time">下单时间：{{ item.created_at }}</text>
						</view>
					</view>
					
					<!-- 第三行：操作按钮 -->
					<view class="card-footer">
						<view class="action-buttons">
							<image class="delete-icon" @click="delEnroll(index)" src="https://xwj-miniprogram.oss-cn-hangzhou.aliyuncs.com/static/images/gd-mr-sc.png" mode=""></image>
							<view class="cancel-btn" @click="cancelEnroll(index)" v-if="item.status==1">
								<text class="cancel-text">取消报名</text>
							</view>
							<view class="note-btn" @click="openApp">
								<text class="note-text">去发笔记</text>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			enrollList: []
		}
	},
	onLoad(options) {
		// 页面加载时获取报名列表
		this.getSignupList()
	},
	methods: {
		// 返回首页
		goHome() {
			uni.navigateTo({
				url: '/pages/garden/map'
			})
		},
		
		// 获取报名列表
		async getSignupList() {
			try {
				const response = await this.$request.get('/api/activity/enroll_list')
				if (response.code === 200 && response.data && response.data.length > 0) {
					// 处理接口返回的数据，转换为页面需要的格式
					this.enrollList = response.data;
				} else {
					// 如果没有数据，显示空列表
					this.enrollList = []
				}
			} catch (error) {
				console.error('获取报名列表失败:', error)
				uni.showToast({
					title: '获取报名列表失败',
					icon: 'none'
				})
				// 出错时显示空列表
				this.enrollList = []
			}
		},
		
		// 格式化状态显示
		formatStatus(status) {
			switch(status) {
				case 1:
					return '待使用'
				case 2:
					return '已完成'
				case 3:
					return '已取消'
				default:
					return '未知状态'
			}
		},
		
		// 获取状态样式类名
		getStatusClass(status) {
			switch(status) {
				case 1:
					return 'status-pending'
				case 2:
					return 'status-completed'
				case 3:
					return 'status-cancelled'
				default:
					return ''
			}
		},
		
		// 删除报名记录
		async delEnroll(index) {
			const item = this.enrollList[index]
			uni.showModal({
				title: '确认删除',
				content: '确定要删除这条报名记录吗？',
				success: async (res) => {
					if (res.confirm) {
						try {
							// 调用删除接口
							const response = await this.$request.post('/api/activity/enroll_status',{id:item.id,status:2})
							if (response.code === 200) {
								// 删除成功，从列表中移除该项
								this.enrollList.splice(index, 1)
								uni.showToast({
									title: '删除成功',
									icon: 'success'
								})
							} else {
								uni.showToast({
									title: response.message || '删除失败',
									icon: 'none'
								})
							}
						} catch (error) {
							console.error('删除报名记录失败:', error)
							uni.showToast({
								title: '删除失败，请重试',
								icon: 'none'
							})
						}
					}
				}
			})
		},
		async cancelEnroll(index) {
			const item = this.enrollList[index]		
			try {
				// 调用删除接口
				const response = await this.$request.post('/api/activity/enroll_status',{id:item.id,status:3})
				if (response.code === 200) {
					this.enrollList[index].status = 3;
					uni.showToast({
						title: '取消成功',
						icon: 'success'
					})
				} else {
					uni.showToast({
						title: response.message || '取消失败',
						icon: 'none'
					})
				}
			} catch (error) {
				console.error('取消报名失败:', error)
				uni.showToast({
					title: '取消失败，请重试',
					icon: 'none'
				})
			}
		},
		// 去发笔记
		openApp() {
			const url = "https://m.ix5j.cn";;
			uni.navigateTo({
				url: '/pages/webview/index?url=' + url
			})
			return
		},
	}
}
</script>

<style lang="scss" scoped>
.garden-page {
	width: 100%;
	min-height: 100vh;
	background: #F7F7F7;
	text-align: center;
	position: relative;
}

.header {
	position: fixed;
	top: 0;
	left: 0;
	right: 0;
	z-index: 999;
	background: #F7F7F7;
	padding: 108rpx 0rpx 40rpx 0rpx;
	
	.gd-icon {
		margin-top: -13rpx;
		width: 64rpx;
		height: 64rpx;
		position: absolute;
		left: 12rpx;
	}
	
	.title {
		font-size: 34rpx;
		font-weight: bold;
		color: #333;
	}
}

.content {
	flex: 1;
	padding: 188rpx 32rpx 342rpx 32rpx;
}

.registration-list {
	display: flex;
	flex-direction: column;
	gap: 32rpx;
}

.registration-card {
	width: 718rpx;
	height: 386rpx;
	background: #FFFFFF;
	border-radius: 24rpx;
	padding: 32rpx;
	box-sizing: border-box;
	display: flex;
	flex-direction: column;
	justify-content: space-between;
}

.card-header {
	display: flex;
	justify-content: space-between;
	align-items: center;
	margin-bottom: 24rpx;
}

.shop-info {
	display: flex;
	align-items: center;
}

.shop-icon {
	width: 32rpx;
	height: 32rpx;
	margin-right: 16rpx;
}

.shop-name {
	font-family: PingFangSC, PingFang SC;
	font-weight: 500;
	font-size: 28rpx;
	color: #262626;
}

.status {
	width: 84rpx;
	height: 36rpx;
	font-family: PingFangSC, PingFang SC;
	font-weight: 500;
	font-size: 28rpx;
	text-align: right;
	font-style: normal;
	text-transform: none;
}

.status-completed {
	color: #FD5219;
}

.status-pending {
	color: #00CAC0;
}

.status-cancelled {
	color: #7B7B7B;
}

.card-content {
	display: flex;
	gap: 24rpx;
	margin-bottom: 24rpx;
}

.activity-image {
	width: 180rpx;
	height: 180rpx;
	border-radius: 16rpx;
	flex-shrink: 0;
}

.activity-info {
	flex: 1;
	display: flex;
	flex-direction: column;
	justify-content: flex-start;
}

.activity-title {
	width: 474rpx;
	height: 80rpx;
	font-family: PingFangSC, PingFang SC;
	font-weight: 500;
	font-size: 32rpx;
	color: #262626;
	text-align: left;
	font-style: normal;
	text-transform: none;
	line-height: 40rpx;
	margin-bottom: 16rpx;
	overflow: hidden;
	text-overflow: ellipsis;
	display: -webkit-box;
	-webkit-line-clamp: 2;
	-webkit-box-orient: vertical;
}

.order-time {
	width: 500rpx;
	height: 30rpx;
	font-family: PingFangSC, PingFang SC;
	font-weight: 400;
	font-size: 24rpx;
	color: #7B7B7B;
	text-align: left;
	font-style: normal;
	text-transform: none;
}

.card-footer {
	display: flex;
	justify-content: flex-end;
	align-items: center;
}

.action-buttons {
	display: flex;
	align-items: center;
	gap: 24rpx;
}

.delete-icon {
	width: 62rpx;
	height: 62rpx;
}

.note-btn {
	width: 168rpx;
	height: 62rpx;
	background: #FFDA61;
	border-radius: 82rpx;
	display: flex;
	align-items: center;
	justify-content: center;
}

.note-text {
	font-family: PingFangSC, PingFang SC;
	font-weight: 500;
	font-size: 24rpx;
	color: #262626;
	text-align: center;
	font-style: normal;
	text-transform: none;
}

.cancel-btn {
	width: 168rpx;
	height: 62rpx;
	background: #FFFFFF;
	border-radius: 82rpx;
	display: flex;
	align-items: center;
	justify-content: center;
	border: 2rpx solid #262626;
}

.cancel-text {
	font-family: PingFangSC, PingFang SC;
	font-weight: 400;
	font-size: 24rpx;
	color: #262626;
	text-align: center;
	font-style: normal;
	text-transform: none;
}
</style>